package cn.stylefeng.guns.modular.recommend.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.recommend.entity.TaoRecommendGoods;
import cn.stylefeng.guns.modular.recommend.model.params.TaoRecommendGoodsParam;
import cn.stylefeng.guns.modular.recommend.service.TaoRecommendGoodsService;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.ResponseMsg;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.UploadFileUtils;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.UploadResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;


/**
 * 推荐商品控制器
 *
 * @author lcw
 * @Date 2019-11-18 14:51:28
 */
@Controller
@RequestMapping("/taoRecommendGoods")
public class TaoRecommendGoodsController extends BaseController {

    private String PREFIX = "/taoRecommendGoods";

    @Autowired
    private TaoRecommendGoodsService taoRecommendGoodsService;

    private static final String SystemConstant = "https://hybn-1259399233.cos.ap-chengdu.myqcloud.com/";

    /**
     * 跳转到主页面
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/taoRecommendGoods.html";
    }

    /**
     * 新增页面
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/taoRecommendGoods_add.html";
    }

    /**
     * 编辑页面
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/taoRecommendGoods_edit.html";
    }

    /**
     * 新增接口
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TaoRecommendGoodsParam taoRecommendGoodsParam, HttpSession session) {
        String images = (String)session.getAttribute("images");
        taoRecommendGoodsParam.setPic(images);
        taoRecommendGoodsService.addrecommendGoods(taoRecommendGoodsParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TaoRecommendGoodsParam taoRecommendGoodsParam) {
        this.taoRecommendGoodsService.update(taoRecommendGoodsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TaoRecommendGoodsParam taoRecommendGoodsParam) {
        this.taoRecommendGoodsService.delete(taoRecommendGoodsParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TaoRecommendGoodsParam taoRecommendGoodsParam) {
        TaoRecommendGoods detail = this.taoRecommendGoodsService.getById(taoRecommendGoodsParam.getGoodId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TaoRecommendGoodsParam taoRecommendGoodsParam) {
        return this.taoRecommendGoodsService.findPageBySpec(taoRecommendGoodsParam);
    }

    /**
     * 添加首页推荐商品
     *
     * @author lcw
     * @Date 2019-11-18
     */
    @ResponseBody
    @RequestMapping("/addGoods")
    public ResponseData addGoods(TaoRecommendGoodsParam taoRecommendGoodsParam) {
        taoRecommendGoodsService.addGoods(taoRecommendGoodsParam);
        return ResponseData.success();
    }

    /**
     * 多图片上传接口
     */
    @RequestMapping("/multipleGraph")
    @ResponseBody
    public ResponseData multipleGraph(@RequestPart("file") MultipartFile file, HttpSession session) throws FileNotFoundException {
        String images = "";
        if (ToolUtil.isEmpty(file)){
            return  ResponseData.error(201,"上传失败，图片为空");
        }else{
            ResponseMsg msg = UploadFileUtils.uploadFile(file);
            if (msg.getStatus() == 10001){
                UploadResult result = (UploadResult) msg.getResult();
                String a  = SystemConstant+result.getPath();
                session.setAttribute("images",SystemConstant+result.getPath());
                return  ResponseData.success(200,"上传成功",result.getPath());
            }else {
                return  ResponseData.error(202,"上传失败，图片格式错误");
            }
        }
    }



}


